greatcfandomcom-20200214-history
Components Hello World
Component Based Design Example 1: hello, world Our initiall specification is as follows. We want a component that can be sustituted for that when run at the bash shell prints the string "hello, world" and terminates. $ hello, world $ Further more we want a test fixture that takes the and tests it. $ hello, world $ We want to create 1 that will test at least 4 different component implementations of . * bash script * c * c# * python Example 1.1: bash For each example discussed here it will be assumed that the reader is familiar with language/environment being used more introductory material can be found on the relevant introductory pages, e.g. Introduction to Bash. * Create the file hello: #!/bin/bash echo hello, world * Set the permissions and run. $ chmod +x hello $ ./hello hello, world $ This doesn't yet meet our specification for a component as it is not yet self contained. We need to install it! $ PATH=$PATH:$(pwd) $ Testing Now that we've got the program working we can start to look at testing and investigate how we might do things differently in the future. To test the simple "hello, world" script we will use the diff programs. diff compares two files to see if they are equal. We need to start with a file that contains the expected output. So we create this: hello, world Save as: expected.txt We then run our script and save the output: ex-01$./hello > actual.txt ex-01$ Now we have the files expected.txt and actual.txt so we compare them with diff: ex-01$diff expected.txt actual.txt ex-01$echo $? 0 ex-01$ The fact that there was no output from diff means the two files are the same. As well as this the special variable $? is 0. $? is the exit status of the last command. To see what happens if the script is incorrect we can edit the script. #!/bin/bash echo hello world We can now run diff again: ex-01$./hello > actual.txt ex-01$diff expected.txt actual.txt 1c1 < hello, world --- > hello world ex-01$echo $? 1 ex-01$ It would be good for automation just to get the return code. We do this by sinking the output from the diff into /dev/vnull ex-01$diff expected.txt actual.txt > /dev/null ex-01$echo $? 1 ex-01$ We now ant to automate our component test. #!/bin/bash PATH=$PATH:$(pwd) hello > actual.txt diff expected.txt actual.txt > /dev/null if $? = 0 then echo PASS else echo FAIL fi -------- file: hellotest Running the test with a bad script: ex-01$./hellotest FAIL ex-01$ And with a good script: ex-01$./hellotest PASS ex-01$ We finish this section by making the test script more general: #!/bin/bash function checkresult () { if $1 = 0 then echo PASS else echo FAIL fi } PATH=$PATH:$(pwd) # # Run component. # $1 > actual.txt # # Test result. # diff expected.txt actual.txt > /dev/null # # Check test result. # checkresult $? Now we can supply the name of the script as an argument for the test. ex-01$./hellotest hello PASS ex-01$ Example 1.2: c int main() { printf("hello, world"); return 0; } Example 1.3: python #!/bin/python print("hello, world") Example 1.4: c# using System; namespace GreatC.Introduction { public class ConsoleApplication { public static void Main(string[] args) { Console.WriteLine("hello, world"); } } } Category:Introduction